package com.dts.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@WebServlet(name = "save", urlPatterns = "/save")
public class FormServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 客户端是以UTF-8编码提交表单数据的，所以需要设置服务器端以UTF-8的编码进行接收，否则对于中文数据就会产生乱码
        request.setCharacterEncoding("UTF-8");
        Map<String, String[]> parameterMap = request.getParameterMap();
        String str = "";
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String key = entry.getKey();
            String[] values = entry.getValue();
            str = str + "<br>" + key + "=" + Arrays.toString(values);
        }
        // 设置服务器端以UTF-8编码输出数据到客户端
        response.setCharacterEncoding("UTF-8");
        // 设置客户端浏览器以UTF-8编码解析数据
        response.setContentType("text/html;charset=UTF-8");
        // 输出htmlStr里面的内容到客户端浏览器显示
        response.getWriter().write(str);
    }
}
